package com.ldc.wandroid.ui.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ldc.common.model.BaseModel;
import com.ldc.common.mvvm.BaseMvvmActivity;
import com.ldc.common.utils.ToastUtils;
import com.ldc.wandroid.adapter.PersonalCoinAdapter;
import com.ldc.wandroid.databinding.ActivityPersonalCoinBinding;
import com.ldc.wandroid.model.PersonalCoinModel;
import com.ldc.wandroid.viewmodel.PersonalCoinViewModel;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;

public class PersonalCoinActivity extends BaseMvvmActivity<ActivityPersonalCoinBinding, PersonalCoinViewModel> {

    private static final String ARG_COIN = "coin";
    private int currCoin = 0;

    public static void actionStart(Activity activity, int currCoin) {
        Intent intent = new Intent(activity, PersonalCoinActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(ARG_COIN, currCoin);
        activity.startActivity(intent);
    }

    private final PersonalCoinAdapter personalCoinAdapter = new PersonalCoinAdapter();
    private int currIndex = 0;
    //
    static final int refresh_dt_code = 0x000;
    static final int refresh_ui_coin_code = 0x001;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewModel.getDate().removeObserver(this::getCoinCountResp);
    }

    public void getCoinCountResp(BaseModel<PersonalCoinModel> dt) {
        if (null == dt) {
            return;
        }
        if (0 == dt.getErrorCode()) {
            Message message = getHandler().obtainMessage(refresh_dt_code);
            message.obj = dt.getData();
            getHandler().sendMessage(message);
        } else {
            ToastUtils.showShort(dt.getErrorMsg());
        }

    }

    @Override
    protected ActivityPersonalCoinBinding initViewBinding() {
        return ActivityPersonalCoinBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        currCoin = getIntent().getIntExtra(ARG_COIN, 0);
        //
        viewBinding.topBar.tvTitle.setText(String.format("%s", "个人积分"));
        viewBinding.topBar.lineMore.setVisibility(View.GONE);
        viewBinding.topBar.lineBack.setOnClickListener(v -> finish());
        //
        initAdapter();
        viewModel.getDate().observeForever(this::getCoinCountResp);
        viewModel.getCoinCountReq(currIndex);
        dynamicShowCoin();
    }


    //显示动态积分
    private void dynamicShowCoin() {
        final Thread thread = new Thread(new Runnable() {
            private int value = 0;

            @Override
            public void run() {
                //判断程序是否被终止
                if (!Thread.currentThread().isInterrupted()) {
                    //遍历数据
                    for (; ; ) {
                        value += 88;
                        Message message = getHandler().obtainMessage(refresh_ui_coin_code);
                        if (value < currCoin) {
                            message.arg1 = value;
                            getHandler().sendMessage(message);
                        } else {
                            message.arg1 = currCoin;
                            getHandler().sendMessage(message);
                            break;
                        }
                        SystemClock.sleep(50);

                    }
                }

            }
        });
        thread.start();

    }

    // 初始化适配器
    private void initAdapter() {
        viewBinding.refreshView.setEnableRefresh(false);//禁止下拉
        viewBinding.refreshView.setOnLoadMoreListener(onLoadMoreListener);
        viewBinding.refreshView.setEnableAutoLoadMore(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        viewBinding.dataList.setHasFixedSize(true);
        viewBinding.dataList.setItemViewCacheSize(10);
        viewBinding.dataList.setLayoutManager(layoutManager);
        viewBinding.dataList.setAdapter(personalCoinAdapter);
    }

    //加载更多
    private final OnLoadMoreListener onLoadMoreListener = refreshLayout -> {
        currIndex += 1;
        viewModel.getCoinCountReq(currIndex);

    };


    @Override
    protected PersonalCoinViewModel initViewModel() {
        return null;
    }
}
